/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataSource;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory;

/**
 *
 * @author Lenovo
 */
public class DatabaseUtil {

    private static final String DRIVER_CLASS;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;
    private static final ResourceBundle rb = ResourceBundle.getBundle("com.qdu.util.dbconfig");

    static {
        DRIVER_CLASS = rb.getString("jdbc.driver");
        URL = rb.getString("jdbc.url");
        USERNAME = rb.getString("jdbc.username");
        PASSWORD = rb.getString("jdbc.password");
    }

    /**
     * 获取数据库连接
     *
     * @return 连接对象
     */
    public static Connection getConnection() {

        Connection con = null;

        try {
            Class.forName(DRIVER_CLASS);//加载驱动类
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);

        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭数据库连接用到的资源
     *
     * @param rs查询的结果集
     * @param stmt查询语句
     * @param con 数据库连接
     */
    public static void close(ResultSet rs, Statement stmt, Connection con) {
        try{
            if(null!=rs)
            {
                rs.close();
            }
            if(null!=stmt)
            {
                stmt.close();
            }
            if(null!=con)
            {
                con.close();
            }
        }catch(SQLException ex){
            ex.printStackTrace();
        }
    }}